<?php
    /* * *************************************************************************
      File:					class.estategent.php
      EstateAgent Improved - a Joomla Component
      Copyright:			2004 - 2006 by Darko Selesi

      Year: 2007
      Improved by: Manos Orfanoudakis - "vosk"
      Homepage: 		http://www.eaimproved.eu
      Version: 			1.3.x
      License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
     * ************************************************************************** */

    defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
    // Get the right languagefile if it exists
    if (file_exists($mosConfig_absolute_path . '/components/com_estateagent/languages/' . $mosConfig_lang . '.php')) {
        include_once($mosConfig_absolute_path . '/components/com_estateagent/languages/' . $mosConfig_lang . '.php');
    } else {
        include_once($mosConfig_absolute_path . '/components/com_estateagent/languages/english.php');
    }
    $base = dirname(__FILE__);
    $base = $base . DIRECTORY_SEPARATOR;
    //Some Queries
    require_once($base . "classes/query.estateagent.php");
    //Main classes
    require_once($base . "classes/image.estateagent.php");
    require_once($base . "classes/object.estateagent.php");
    require_once($base . "classes/journal.estateagent.php");
    require_once($base . "classes/bookmark.estateagent.php");
    require_once($base . "classes/category.estateagent.php");
    //Other Features
    if (file_exists($base . "classes/gmap.estateagent.php"))
        require_once($base . "classes/gmap.estateagent.php");

    require_once($base . "classes/map24.estateagent.php");
    require_once($base . "classes/template.estateagent.php");

    if (!$mainframe->isAdmin()) {//FrontEnd Only
        require_once($base . "classes/helper.estateagent.php");
    }

    $today = mosCurrentDate($format = "%Y-%m-%d");

    class mosEAHTML extends mosHTML {

        // shows a coloured radiobutton-list ------------------------------
        static function radioList($arr, $tag_name, $tag_attribs, $selected=null, $key='value', $text='text', $property=0) {
            reset($arr);
            $html = "";
            for ($i = 0, $n = count($arr); $i < $n; $i++) {
                $k = $arr[$i]->$key;
                $t = $arr[$i]->$text;
                $id = @$arr[$i]->id;

                $extra = "";
                $extra .= $id ? " id=\"" . $arr[$i]->id . "\"" : '';
                if (is_array($selected)) {
                    foreach ($selected as $obj) {
                        $k2 = $obj->$key;
                        if ($k == $k2) {
                            $extra .= " selected=\"selected\"";
                            break;
                        }
                    }
                } else {
                    $extra .= ( $k == $selected ? " checked=\"checked\"" : '');
                }
                $html .= "\n\t<input type=\"radio\" name=\"$tag_name\" value=\"" . $k . "\"" . $tag_attribs . " " . $extra . " /><span style=\"width:35px; border-style:solid;border-color:#666666;border-width:1px;background-color:" . $k . "\">" . $t . "</span>";
            }
            $html .= "\n";
            return $html;
        }

        // shows a single checkbox ----------------------------------
        static function checkbox($name, $tag_attribs, $value, $text='', $showtext=0, $checked=null) {
            $html = "";

            $t = ($showtext == '1') ? "&nbsp" . $text : '';
            $checked = ($checked == 1) ? " checked=\"checked\"" : '';
            $html .= "<input type=\"checkbox\" name=\"" . $name . "\" " . $tag_attribs . " value=\"" . $value . "\"" . $checked . " />" . $t;

            return $html;
        }

        // shows a group of checkboxes ----------------------------------
        static function checkbox_list(&$arr, $tag_name, $tag_attribs, $key='value', $text='text', $checked=array(0, 0, 0, 0, 0)) {
            reset($arr);
            $html = "";

            for ($i = 0, $n = count($arr); $i < $n; $i++) {
                $k = $arr[$i]['key'];
                $t = $arr[$i]['text'];

                $extra = ($checked[$i] == 1) ? " checked=\"checked\" " : "";

                $html .= "<input type=\"checkbox\" name=\"" . $tag_name . "\" value=\"" . $k . "\"" . $extra . " " . $tag_attribs . " />" . $t . "</input>&nbsp;";
            }

            return $html;
        }

        static function emailToFriendLink($oid, $type) {
            global $mosConfig_live_site;

            $html = "";
            $status = 'status=no,toolbar=no,scrollbars=no,titlebar=no,menubar=no,resizable=yes,width=480,height=270,directories=no,location=no';
            $link = $mosConfig_live_site . '/index2.php?option=com_estateagent&amp;act=object&amp;task=mtfform&amp;id=' . $oid;
            if ($type == 2) {
                $image = mosAdminMenus::ImageCheck('emailButton.png', '/images/M_images/', NULL, NULL, _CMN_EMAIL);
            } else {
                $image = '&nbsp;' . _CMN_EMAIL;
            }
            $html .= "<a href=\"javascript:void(0);\" onclick=\"window.open('" . $link . "','EstateAgent','" . $status . "');\" title=\"" . _CMN_EMAIL . "\">";
            $html .= $image;
            $html .= "</a>\n";

            return $html;
        }

        static function check_email($email) {
            $check = false;
            if (preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $email)) {
                $check = true;
            }
            return $check;
        }

        static function printIcon(&$obj, &$type, $p1 = null, $p2 = null, $p3 = null) {
            global $mosConfig_live_site;
            $text = "<a href=\"javascript:void(0);\" onmousedown=\"window.open('" .
                    sefRelToAbs(ampReplace("index2.php?option=com_estateagent&amp;act=object&amp;task=print&amp;id=$obj")) .
                    "', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=800,height=600,directories=no,location=no');\" title=\"Print\">";

            //	$text .= "<img src=\"".$mosConfig_live_site."/images/M_images/printButton.png\" alt=\"Print\" /></a>";
            if ($type == 2) {
                $text.= mosAdminMenus::ImageCheck('printButton.png', '/images/M_images/', NULL, NULL, _CMN_PRINT);
            }
            else
                $text .= _CMN_PRINT;
            $text .="</a>";
            return $text;
        }

        static function arrowIcon() {
            return mosAdminMenus::ImageCheck('arrow.png', '/images/M_images/', NULL, NULL, "Arrow", NULL, 1, NULL, NULL, NULL);
        }

        static function catSelectList($tag, $attrib, $sel=null) {

            $options[0] = mosHTML::makeOption('0', _EAB_CAT_OPT, "key", "text");
            $options = array_merge($options, mosEAHTML::subcatSelect(0, ""));

            return mosHTML::selectList($options, $tag, $attrib, "key", "text", $sel);
        }

        static function subcatSelect($parent, $prefix) {
            global $database;
            $options = array();
            $q = EAQ::getCategories($parent);
            //echo $q;

            $database->setQuery($q);
            $result = $database->loadObjectList();
            $total = count($result);
            for ($i = 0; $i < $total - 1; $i++) {
                $options[] = mosHTML::makeOption($result[$i]->id, $prefix . "->" . $result[$i]->name, "key", "text");
                $options = array_merge($options, mosEAHTML::subcatSelect($result[$i]->id, $prefix . "&nbsp;&nbsp;&nbsp;"));
            }
            if ($total > 0) {

                $options[] = mosHTML::makeOption($result[$total - 1]->id, $prefix . "->" . $result[$total - 1]->name, "key", "text");
                $options = array_merge($options, mosEAHTML::subcatSelect($result[$total - 1]->id, $prefix . "&nbsp;&nbsp;&nbsp;"));
            }
            return $options;
        }

    }

    // generates the locationsearch querystring
    function location_srch_query() {
        global $database;
        $parts = array();
        //$country=	$database->getEscaped(mosGetParam($_REQUEST,'src_country',null));
        $state = $database->getEscaped(mosGetParam($_REQUEST, 'src_state', null));
        $town = $database->getEscaped(mosGetParam($_REQUEST, 'src_town', null));
        $district = $database->getEscaped(mosGetParam($_REQUEST, 'src_district', null));

        //if($country!=null&&$country!="no")
        //$parts[]= " country = '".$country."' ";
        if ($state && $state != "no")
            $parts[] = " locstate = '" . $state . "' ";
        if ($town && $town != "no")
            $parts[] = " town = '" . $town . "' ";
        if ($district && $district != "no")
            $parts[] = " district = '" . $district . "' ";

        if (count($parts) > 0) {
            return " ( " . implode(" AND ", $parts) . " ) ";
        }
        else
            return "";
    }

    function detailed_srch_query() {
        $details = (isset($_REQUEST["atb"]) ? $_REQUEST["atb"] : array() );
        $parts = array();
        foreach (@$details as $d) {
            if (!get_magic_quotes_gpc()) {
                $d = addslashes($d);
            }
            $parts[] = "$d=1";
        }

        if (count($parts) > 0) {
            return " ( " . implode(" AND ", $parts) . " ) ";
        }
        else
            return "";
    }

    // generates the extendedsearch querystring
    function extended_srch_query() {
        global $database;
        $parts = array();
        $space = $database->getEscaped(mosGetParam($_REQUEST, 'space', 0));
        switch ($space) {
            case 1:
                $parts[] = "space<='50'";
                break;
            case 2:
                $parts[] = "space>='40' AND space<='80'";
                break;

            case 3:
                $parts[] = "space>='70' AND space<='120'";
                break;
            case 4:
                $parts[] = "space>='110' AND space<='160'";
                break;
            case 5:
                $parts[] = "space>='150' AND space<='220'";
                break;
            case 6:
                $parts[] = "space>='200' AND space<='300'";
                break;
            case 7:
                $parts[] = "space>'300'";
                break;
            default:
                break;
        }

        $year = date("Y");
        $yobuilt = $database->getEscaped(mosGetParam($_REQUEST, 'yobuilt', 0));
        switch ($yobuilt) {
            case 1:
                $parts[] = "yobuilt='" . $year . "'";
                break;
            case 2:
                $parts[] = "yobuilt>'" . $year = ($year - 10) . "'";
                break;

            case 3:
                $parts[] = "yobuilt>'" . $year = ($year - 20) . "'";
                break;
            case 4:
                $parts[] = "yobuilt>'" . $year = ($year - 40) . "'";
                break;
            case 5:
                $parts[] = "yobuilt>'" . $year = ($year - 60) . "'";
                break;
            case 6:
                $parts[] = "yobuilt<'" . $year = ($year - 80) . "'";
                break;
            case 7:
                $parts[] = "yobuilt<'" . $year = ($year - 80) . "'";
                break;
            default:
                break;
        }

        $rooms = $database->getEscaped(mosGetParam($_REQUEST, 'rooms', 0));
        switch ($rooms) {
            case 1:
                $parts[] = "rooms='1'";
                break;
            case 2:
                $parts[] = "rooms>='2'";
                break;

            case 3:
                $parts[] = "rooms>='3'";
                break;
            case 4:
                $parts[] = "rooms>='4'";
                break;
            case 5:
                $parts[] = "rooms>='5'";
                break;
            case 6:
                $parts[] = "rooms>='6'";
                break;
            case 7:
                $parts[] = "rooms>='7'";
                break;
            default:
                break;
        }

        $bedrooms = $database->getEscaped(mosGetParam($_REQUEST, 'bedrooms', 0));
        switch ($bedrooms) {
            case 1:
                $parts[] = "bedrooms='1'";
                break;
            case 2:
                $parts[] = "bedrooms>='2'";
                break;

            case 3:
                $parts[] = "bedrooms>='3'";
                break;
            case 4:
                $parts[] = "bedrooms>='4'";
                break;
            case 5:
                $parts[] = "bedrooms>='5'";
                break;
            case 6:
                $parts[] = "bedrooms>='6'";
                break;
            case 7:
                $parts[] = "bedrooms>='7'";
                break;
            default:
                break;
        }
        if (count($parts) > 0) {
            return " ( " . implode(" AND ", $parts) . " ) ";
        }
        else
            return "";
    }

    /* Compile search query */

    function searchQuery() {
        global $eaconf, $database;
        $parts = array();

        if ($eaconf->ea_l_srch == 1) {
            $country = $database->getEscaped(mosGetParam($_REQUEST, 'src_country', null));
            if ($country != null && $country != "no")
                $parts[] = " country = '" . $country . "' ";
        }
        $temp = extended_srch_query();
        if ($temp != "" && $temp != null && $eaconf->ea_d_srch == 1)
            $parts[] = $temp;

        $temp = detailed_srch_query();
        if ($temp != "" && $temp != null && $eaconf->ea_d_srch == 1)
            $parts[] = $temp;
        /* echo("<script>alert('".implode(" AND " , $parts)."');</script>"); */
        $type = $database->getEscaped(mosGetParam($_REQUEST, 'type', null));
        if ($type == 1)//Enikoiash
            $parts[] = "(type = '1')";
        else if ($type == 2)//Pwlhsh
            $parts[] = "(type = '2')";
        else if ($type == 3)//Pwlhsh
            $parts[] = "(type = '3')";
        else if ($type == 4)//Pwlhsh
            $parts[] = "(type = '4')";
        return implode(" AND ", $parts);
    }

    // loads the locations for generatng the location selectfields for the searchform
    function getMap() {
        global $database;

        $query = "SELECT id,country, locstate, town, district FROM #__estateagent WHERE published=1";
        $database->setQuery($query);
        $result = $database->loadObjectList();
        echo $database->getErrorMsg();

        return $result;
    }

    class EAConf {

        function EAConf() {
            global $mosConfig_absolute_path;
            require($mosConfig_absolute_path . "/administrator/components/com_estateagent/configuration.php");
            $allvars = get_defined_vars();
            //print_r($allvars);
            $names = array_keys($allvars);
            foreach ($names as $name) {
                if (substr($name, 0, 2) == "ea")
                    $this->$name = $allvars[$name];
            }
            $this->ea_version = "1.3.6 "; //=====

            if ($this->ea_nformat == 0) {
                $this->sep1 = ".";
                $this->sep2 = ",";
            } else {
                $this->sep1 = ",";
                $this->sep2 = ".";
            }
            $base = dirname(__FILE__);
            $base = $base . DIRECTORY_SEPARATOR;
            if (!file_exists($base . "classes/gmap.estateagent.php"))
                $this->ea_googlemap_enable = 0;
            if ($this->ea_disperrors) {
                ini_set('display_errors', '1');
                error_reporting(2047);
            }
        }

    }

    $GLOBALS['eaconf'] = new EAConf();
    ?>
